#include "PlayerAction.h"
void PlayerAct(Object *pCursor, char *pMap, int *Still)
  {
    char Key = 0;
    if (_kbhit())
      Key = _getch();
    if (CouldGo(pCursor, pMap, Key))
      {
        MoveInArr(pCursor, Key);
        MoveOnMap(pCursor, Key);
      }

    if (Key == 'w')
      JumpForward(pCursor);
    if (Key == 'b')
      JumpBackward(pCursor);
    if (Key == 'A')
      JumpEOL(pCursor);
    if (Key == '0')
      JumpTOL(pCursor);

    if (Key == 113)//q
      {
        WriteStatus('M');
        *Still = 0;
      }
    if (Key == 27)//ESC
      {
        WriteStatus('M');
        *Still = 0;
      }
  }

void MouseCursor(Object *pCursor, int *Still)
  {
    MOUSEMSG mouse;
    mouse = GetMouseMsg();
    pCursor->X = mouse.x, pCursor->Y = mouse.y;
    if (mouse.uMsg == WM_RBUTTONDOWN)
      *Still = 0;
  }

int CouldGo(Object *pCursor, char *pMap, char Key)
  {
    int GridX= pCursor->GridX, GridY = pCursor->GridY;

    if(Key == 'k')//Up
        GridY = pCursor->GridY - 1;
    if(Key == 'j')//Down
        GridY = pCursor->GridY + 1;
    if (Key == 'h')//Left
        GridX = pCursor->GridX - 1;
    if (Key == 'l')//Right
        GridX = pCursor->GridX + 1;

    int Serial = GridY * 25 + GridX;

    if (Serial < 350)//Avoid overread of array
      {
        if (pMap[Serial] == 'T')//Empty
          return 1;
        else//Barrier
          return 0;
      }
  }

void MoveInArr(Object *pCursor, char Key)
  {
    if(Key == 'k')//Up
        pCursor->GridY = pCursor->GridY - 1;

    if(Key == 'j')//Down
        pCursor->GridY = pCursor->GridY + 1;

    if (Key == 'h')//Left
        pCursor->GridX = pCursor->GridX - 1;

    if (Key == 'l')//Right
        pCursor->GridX = pCursor->GridX + 1;
  }
void MoveOnMap(Object *pCursor, char Key)
  {
    if(Key == 'k')//Up
        pCursor->Y = pCursor->Y - 50;

    if(Key == 'j')//Down
        pCursor->Y = pCursor->Y + 50;

    if (Key == 'h')//Left
        pCursor->X = pCursor->X - 50;

    if (Key == 'l')//Right
        pCursor->X = pCursor->X + 50;

  }

void JumpTo(Object *pCursor, int GridX, int GridY)
  {
    pCursor->GridX = GridX;
    pCursor->GridY = GridY;
    pCursor->X = GridX * 50 + 15;
    pCursor->Y = GridY * 50 + 10;
  }
int JumpForward(Object *pCursor)
  {
    int GridX = pCursor->GridX, GridY = pCursor->GridY;
    if (GridX == 4)
      if (GridY == 2)
        {
          JumpTo(pCursor, 11, 2);
          return 1;
        }
    if (GridX == 11)
      if (GridY == 2)
        {
          JumpTo(pCursor, 14, 2);
          return 1;
        }
    if (GridX == 14)
      if (GridY == 2)
        {
          JumpTo(pCursor, 18, 2);
          return 1;
        }
    if (GridX == 18)
      if (GridY == 2)
        {
          JumpTo(pCursor, 21, 2);
          return 1;
        }
    if (GridX == 0)
      if (GridY == 3)
        {
          JumpTo(pCursor, 7, 3);
          return 1;
        }
  }
int JumpBackward(Object *pCursor)
  {
    int GridX = pCursor->GridX, GridY = pCursor->GridY;
    if (GridX == 11)
      if (GridY == 2)
        {
          JumpTo(pCursor, 4, 2);
          return 1;
        }
    if (GridX == 14)
      if (GridY == 2)
        {
          JumpTo(pCursor, 11, 2);
          return 1;
        }
    if (GridX == 18)
      if (GridY == 2)
        {
          JumpTo(pCursor, 14, 2);
          return 1;
        }
    if (GridX == 21)
      if (GridY == 2)
        {
          JumpTo(pCursor, 18, 2);
          return 1;
        }
    if (GridX == 7)
      if (GridY == 3)
        {
          JumpTo(pCursor, 0, 3);
          return 1;
        }
  }
int JumpEOL(Object *pCursor)
  {
    int GridX = pCursor->GridX, GridY = pCursor->GridY;
    if (GridX == 2)
      if (GridY == 6)
        {
          JumpTo(pCursor, 17, 7);
          return 1;
        }
    if (GridX == 20)
      if (GridY == 8)
        {
          JumpTo(pCursor, 7, 11);
          return 1;
        }
  }
int JumpTOL(Object *pCursor)
  {
    int GridX = pCursor->GridX, GridY = pCursor->GridY;
    if (GridX == 17)
      if (GridY == 7)
        {
          JumpTo(pCursor, 2, 6);
          return 1;
        }
    if (GridX == 7)
      if (GridY == 11)
        {
          JumpTo(pCursor, 20, 8);
          return 1;
        }
  }

void GridXY2XY(int GridX, int GridY, int *X, int *Y)
  {
    *X = GridX * 50 + 15;
    *Y = GridY * 50 + 10;
  }
